from apps.utils.enum import PendingStatus


class DriftViewModel:
    def __init__(self, data, current_user_id):
        self.data = data
        self.drift = self.__parse(current_user_id)

    @staticmethod
    def is_requester(current_user_id, drift):
        return current_user_id == drift.requester_id

    def __parse(self, current_user_id):
        identification = 'requester' if self.is_requester(current_user_id, self.data) else 'gifter'
        r = dict(
            book_img=self.data.book_img,
            book_title=self.data.book_title,
            book_author=self.data.book_author,
            date=self.data.create_time,
            operator=self.data.gifter_nickname if self.is_requester(
                current_user_id, self.data) else self.data.requester_nickname,
            recipient_name=self.data.recipient_name,
            address=self.data.address,
            mobile=self.data.mobile,
            status_str=PendingStatus.pending_str(self.data.pending, identification),
            identification=identification,
            status=self.data.pending,
            drift_id=self.data.id
        )
        return r


class DriftViewCollection:
    def __init__(self, data, current_user_id):
        self.data = data
        self.drifts = self.__parse(current_user_id)

    def __parse(self, current_user_id):
        return [DriftViewModel(data, current_user_id).drift for data in self.data]

